/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CaseJumpSearchDetailCtrl', ['$scope','$http','$modal','BaseURL','Modal','CaseCommonService','WarningAddService','FwaTabsetService',
	                                        'AuditDictServices','$stateParams','$state','WarningScreenService',
	                                function($scope,$http,$modal,baseUrl,Modal,CaseCommonService,WarningAddService,fwaTabsetService,
	                                		AuditDictServices,$stateParams,$state,WarningScreenService){
		
		
		
		$scope.problemProcessOpinion={}; // 处理意见
		$scope.problemAuditOpinion={}; // 审核意见
		$scope.surveyResult={}; // 调查详情
		$scope.surveyReview={}; // 调查复核
		$scope.surveyAudit={}; // 调查审核
		$scope.auditProjectList=[]; // 稽核项目列表
		$scope.caseCheckResult={}; // 稽核结果
		$scope.caseReviewResult={}; // 稽核复核结果
		$scope.caseApproveResult={}; // 稽核结论审批意见
		$scope.caseAuditResult={}; // 稽核审核意见 13
		$scope.auditDictServices = AuditDictServices;
        AuditDictServices.initCodeListDatas();
        $scope.reportBean={}; //稽核报告
		$scope.caseDetail={}; //稽核告知函
        $scope.auditSummary = null;//不合理费用
        $scope.ammountCount = 0;//不合理费用
		$scope.dispatchButton=true;
		$scope.confirmButton=true;
		
		$scope.goBackBtnShow=false;
		
		
//		$scope.reportShow=true; //展现稽核报告和稽核告知汉
		
		
		$scope.auditObj={}; //审核对象
		
		$scope.finishNode = 7;
		
		var tabSource = [
		             {label:'稽核项目',id:2,status:3,url:'views/hcpms/audit/caseCheck/caseCheckAuditPorject_view.html'},
		             {label:'稽核结果',id:3,status:3,url:'views/hcpms/audit/caseSearch/caseCheckAuditResult_view.html'},
		             {label:'审批意见',id:4,status:6,url:'views/hcpms/audit/caseSearch/caseAduitCaseAprmAdd_view.html'}];
		
		$scope.problemStatus = 7;
		
        $scope.problemtab=[{label:'事件处理',id:0,status:1,url:'views/hcpms/audit/problemSearch/caseCheckProblemDeal.html'},
      		             {label:'调查结果',id:1,status:3,url:'views/hcpms/audit/problemSearch/caseCheckSurveyResult.html'}];

        $scope.tabClicks = function(tab) {
            $scope.selectedTabs = tab;
            $scope.tabIndex = tab.id;
            setTimeout(function(){
				$(window).trigger('resize');
            },0);
        }
//        $scope.tabClicks($scope.problemtab[4]);
        
        
        $scope.columns = [
                          {label:'项目名称',map:'projectName',width:'20%'},
                          {label:'结算方式',map:'treatTypeName',width:'20%'},
                          {label:'不合理费用',map:'deductionAmmountStr',width:'20%'},
                          {label:'备注',map:'remark',width:'40%'}
                         ];
       $scope.tableData = [];
       
//       $scope.selectList=[{code:'1',label:'同意'},{code:'0',label:'重新稽核'}];
       
          //初始化请求参数
	    function params(){
			var caseCode = "";
			if(!angular.isUndefined($stateParams.caseCode) && $stateParams.caseCode){
				caseCode = $stateParams.caseCode;
				$scope.caseCode=caseCode;
				$scope.problemCode=$stateParams.problemCode;
				
			}
			var params = {
					caseCode:caseCode,
					problemCode:$scope.problemCode
			}
			return params;
		}
       
	    /**
	     * 是否展示复核意见  3:复核  4:审核
	     */
	    $scope.chuckOpione = 6;
	    
	    $scope.caseStatus = 0 ;
	    
       /**
        * 初始化案件对象
        */
       function initCaseInfo(){
    	   var parameter=params();
    	   CaseCommonService.loadCaseData(parameter).success(function(response){ 
    		   //处理tab页
    		   var _obj = response.result.case1;
               //_obj.otherCaseCode=_obj.relatedCode;
    		   $scope.auditSummary = _obj.auditSummary;
    		   //处理小数点
    		   var str = parseFloat(_obj.totalDeduction)+"";
	       		var la = str.split(".");
	       		if(la.length==1){
	       			str+=".00";
	       		}
    		   $scope.totalDeduction = str;
    		   _obj.problemStart = _obj.periodBeginDate;
    		   _obj.problemEnd = _obj.periodEndDate;
    		   _obj.problemNo = _obj.relatedCode;
    		   $scope.object= _obj;
    		   $scope.caseStatus = _obj.caseState;
    		   //初始化稽核tab 页面 需要依赖  主信息状态
    		   initCaseTabInfo();
    		   if(!angular.isUndefined($stateParams.caseStatus) && $stateParams.caseStatus){
    			   if(_obj.caseState == $stateParams.caseStatus){
    				   $scope.goBackBtnShow = true;
    			   }
    		   }
			});
       }
       
       /**
        * 初始化医疗机构,医务人员,参保人信息
        */
       function initMedicalAndStaffAndInsurance(){
    	   var obj=params();
    	   var parameter={
    			   problemNo:obj.caseCode,
    			   taskType:'2'             //案件
    	   };
			CaseCommonService.loadMedicalInfoList(parameter).success(function(response){   	 
				$scope.medicalData2=response.medicalBean.medicalList;
				$scope.staffData2=response.staffBean.staffList;
				$scope.insuranceData2=response.insuranceBean.insuranceList;
				WarningScreenService.findPointCutTempScose(
						WarningAddService.warningMarks,$stateParams.caseCode,'02',
	     				  $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);

			});
       }
       /**
        * 初始化各个tab 审批意见信息
        */
       function initTabInfo(){
    	   var param = {caseCode:$stateParams.caseCode,problemNo:$stateParams.problemCode};
    	   CaseCommonService.queryAllOpinion(param).success(function(response){   	 
    		   resetEachTabData(response);
			});
       }
       
   	/**
      * 0-初始任务分配1-调查初审
		2-事件审核3-调查结论4-调查结论复核
		5-调查结论审核
		10-稽核分配
		11-稽核结论
		12-稽核结论复核
		13-稽核结论审核
		14-医疗机构确认稽核结论
		15-稽核结论审批
   	 */
       function resetEachTabData(_array){
    	   if(_array!= null && _array.length >0){
    		   angular.forEach(_array,function(data,index){
    			   if(data.opinionType == 1){
    				   $scope.problemProcessOpinion = data;
    			   }else if(data.opinionType == 2){
    				   $scope.problemAuditOpinion = data;
    			   }else if(data.opinionType == 3){
    				   $scope.surveyResult = data;
    			   }else if(data.opinionType == 4){
    				   $scope.surveyReview = data;
    			   }else if(data.opinionType == 5){
    				   $scope.surveyAudit = data;
    			   }else if(data.opinionType == 15){
    				   $scope.caseApproveResult = data;
    			   } 
    			   
    		   });
    	   }
       }
       
       /**
        * 初始化稽核告知函的报告
        */
       function ininAuditNotifiaction(){
       	var parameter={
       			caseCode:$stateParams.caseCode
       	};
         CaseCommonService.loadAuditNotifionInfo(parameter).success(function(response){   
       	  
       	  
       	  
				$scope.reportBean=response.reportBean;
				$scope.caseDetail=response.tellReportBean;
				
				$scope.reportBean.auditRang={
		       			   start:$scope.reportBean.auditStart,
		       			   end:$scope.reportBean.auditEnd
		       	      };
			});
       }
       
       /**
        * 展现稽核报告对话框
        */
       $scope.showCaseAuditReportDialog=function(){
       	var scope = $scope.$new();
       	scope.caseCode=$scope.caseCode;
       	 CaseCommonService.showCaseAuditReportDialog(scope);
       }
       
       /**
        * 展现稽核告知函
        */
       $scope.showCaseAuditTellReportDialog=function(){
    	var scope = $scope.$new();
       	scope.caseCode = $scope.caseCode;
       	CaseCommonService.showCaseAuditTellReportDialog(scope);
       }
       
       /**
        * 
        * @param response
        */
       function caseResponseResult(response){
    	   if(response.auditProjectList){
    		   $scope.tableData=response.auditProjectList;
    	   }
    	   if(response.caseReviewResult){
    		   $scope.caseReviewResult=response.caseReviewResult;
    	   }
    	   
    	   if(response.caseApproveResult){
    		   $scope.caseApproveResult=response.caseApproveResult;
    	   }
    	   // 稽核审核意见 13
    	   if(response.caseAuditResult){
    		   $scope.caseAuditResult=response.caseAuditResult;
    		   if(response.caseAuditResult.opinion == "1"){
    			   //第四个环节结束掉
    			   $scope.finishNode = 4;
    		   }
    	   }
    	   if(response.caseCheckResult){
    		   $scope.caseCheckResult=response.caseCheckResult;
    		   if($scope.caseCheckResult.caseResult=='0'){ 
    			 //稽核结论: 异常情况需要走审批
    			   $scope.finishNode = 7;
    		   } 
    	   }
    	   
    	   var caseState = $scope.caseStatus;
    	   
    	   if(caseState == 7){
			   if($scope.finishNode == 4){
				   //从审批过程就跳节点
				   $.each(tabSource,function(index,data){
					   if($scope.finishNode > data.status){
						   $scope.problemtab.push(data);
					   }
				   })
				   $scope.tabClicks($scope.problemtab[3]);
			   }else{
				   //已经结束
				   $scope.problemtab = $scope.problemtab.concat(tabSource);
				   $scope.tabClicks($scope.problemtab[4]);
			   }
			   
		   }else if(caseState == 6){
			   $.each(tabSource,function(index,data){
				   if(caseState > data.status){
					   $scope.problemtab.push(data);
				   }
			   })
			   $scope.tabClicks($scope.problemtab[3]);
		   }else if(caseState == 3){
			   //复核的情况下 
			   $.each(tabSource,function(index,data){
				   if(data.status < 6){
					   $scope.problemtab.push(data);
				   }
			   })
			   $scope.tabClicks($scope.problemtab[3]);
			   $scope.chuckOpione = 3;
		   }else if(caseState == 4){
			   $.each(tabSource,function(index,data){
				   if(data.status < 6){
					   $scope.problemtab.push(data);
				   }
			   })
			   $scope.tabClicks($scope.problemtab[3]);
			   $scope.chuckOpione = 4;
		   }else if(caseState > 4){
			   $.each(tabSource,function(index,data){
				   if(data.status < 6){
					   $scope.problemtab.push(data);
				   }
			   })
			   $scope.tabClicks($scope.problemtab[3]);
		   }else {
			   $scope.tabClicks($scope.problemtab[1]);
		   }
       }
       

       /**
        * 返回事件
        */
       $scope.goBack = function(){
    	   if(!angular.isUndefined($stateParams.backFlag) && $stateParams.backFlag){
    		   $state.go($stateParams.backFlag);
		   }else{
			   $state.go('caseSearch');
		   }
    	   
       }
       
       
       /**
        * 查看案件信息对象中案件来源对话框
        */
       $scope.$on('fwaAuditLeft.showModal',function(){
    	   $scope.caseObj=$scope.object;
    	   CaseCommonService.showCaseFromSourceDialog($scope);
       });
       
       
       /**
        * 点击医疗机构量事件
        * 
        */
       $scope.$on('fwaInvolve.medicalCountClick',function(){
    	   CaseCommonService.viewMedicalDetail($scope);
       });
       
       /**
        * 点击医务人员总数量事件
        */
       $scope.$on('fwaInvolve.staffCountClick',function(){
    	   CaseCommonService.viewStaffList($scope);
       });
       
       /**
        * 点击参保人总数量事件
        */
       $scope.$on('fwaInvolve.insuranceCountClick',function(){
       		CaseCommonService.viewInsurList($scope);
       });
       
       /**
        * 初始化各个tab 信息
        */
       function initCaseTabInfo(){
    	 var parameteObject=params();
    	   $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'caseCheckAudit/loadCaseInfo.do',
				params :parameteObject
			}).success(function(response, status, headers, config) {


				caseResponseResult(response);

			});
    	   
       }

       /**
	    * 撤回
	    * 
	    */ 
       $scope.returnApr = function(){
    	   if($scope.caseStatus==$stateParams.caseStatus){
    		   $scope.loading = !$scope.loading;
    	      	 var premer={
    	      			 code:$stateParams.caseCode,
    	      			 status:$stateParams.caseStatus,
    	      			 type:"02"
    	      	 }
    	          CaseCommonService.caseRollBack(premer).success(
    		           function(response, status, headers, config){
    		        	   $scope.loading = !$scope.loading;
    		        	   Modal.alert(response.result.message);
    		        	   $scope.goBack();
    		            });
    	   }else{
    		   Modal.alert("非法操作,请刷新页面再操作!");
    	   }
       }
       
       /**
        * 初始化所有的数据
        */
       function initAllData(){
    	  initCaseInfo();
    	  initMedicalAndStaffAndInsurance();
    	  initTabInfo();
       }
       
       initAllData();
     
       
     
       
       
       
       // 	end line	
	}]);
})();